#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.api.controller;

import ${package}.common.dto.ResultDto;
import ${package}.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;

/**
 * 异常处理
 *
 * @author tqccc
 */
@Slf4j
@RestControllerAdvice
public class ExceptionHandlerResolver {

    /**
     * 全局异常.
     *
     * @param e the e
     * @return ResultDto
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public ResultDto<String> handleGlobalException(Exception e) {
        log.error("全局异常", e);
        return ResultDto.failed(e.getLocalizedMessage());
    }

    /**
     * 业务异常.
     *
     * @param e the e
     * @return ResultDto
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.OK)
    public ResultDto<Object> handleBusinessException(BusinessException e) {
        log.error("全局业务异常", e);
        return ResultDto.failed()
                .setCode(e.getCode())
                .setMsg(e.getMessage());
    }

    /**
     * AccessDeniedException
     *
     * @param e the e
     * @return ResultDto
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ResultDto<String> handleAccessDeniedException(AccessDeniedException e) {
        String msg = SpringSecurityMessageSource.getAccessor()
                .getMessage("AbstractAccessDecisionManager.accessDenied"
                        , e.getMessage());
        log.error("拒绝授权异常信息", e);
        return ResultDto.failed(e.getLocalizedMessage());
    }

    /**
     * validation Exception
     *
     * @param exception e
     * @return ResultDto
     */
    @ExceptionHandler(BindException.class)
    @ResponseStatus(HttpStatus.OK)
    public ResultDto<String> handleBodyValidException(BindException exception) {
        List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
        log.error("参数绑定异常,ex = {}", fieldErrors.get(0).getDefaultMessage());
        return ResultDto.failed(fieldErrors.get(0).getDefaultMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.OK)
    public ResultDto<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        String msg = e.getBindingResult().getAllErrors().get(0).getDefaultMessage();
        log.error("参数绑定异常,ex = {}", msg);
        return ResultDto.failed(msg);
    }

}
